//
//  WebViewController.m
//  eNew
//
//  Created by lanou3g on 15-4-29.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "WebViewController.h"
#import "ButtonView.h"
#import "FMDB.h"


@interface WebViewController ()<UIWebViewDelegate>

@property (nonatomic, retain) UIWebView *webView;
@property (nonatomic, retain) ButtonView *buttonView;

@end

@implementation WebViewController

- (void)viewDidLoad {
    
    self.webView = [[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 375, 618)] autorelease];
    NSURL *url = [NSURL URLWithString:_url];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:request];
    [self.view addSubview:_webView];
    
    self.buttonView = [[[ButtonView alloc] initWithFrame:CGRectMake(0, 618, 375, 49)] autorelease];
    [self.view addSubview:_buttonView];
    
    self.navigationItem.rightBarButtonItem = ({
        [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"font"] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarButtonItemAction:)];
    });
    
    // 给button添加事件
    [self.buttonView.shareButton addTarget:self action:@selector(shareButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonView.collectButton addTarget:self action:@selector(collectButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonView.blackButton addTarget:self action:@selector(blackButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonView.reportButton addTarget:self action:@selector(reportButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark 实现button方法
- (void)shareButtonAction:(UIButton *)sender {
    
}

- (void)collectButtonAction:(UIButton *)sender {
    //拿着url去数据库中查找，
    //_url  _name;
    
    NSString *filthPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    filthPath = [filthPath stringByAppendingPathComponent:@"colletion.sqlite"];
    NSLog(@"%@",filthPath);
    FMDatabase  *db = [FMDatabase databaseWithPath:filthPath];
    if (![db open]) {
        NSLog(@"打开数据库失败");
        [db release];
        return;
    }
    // 创建数据表
    [db executeUpdate:@"create table collection (Name text Primary Key , url text)"];
    // 判断是否插入
    BOOL i = [db executeUpdate:@"insert into collection(Name, url) values (?,?)",_name,_url];
    if (i != 0) {
        NSLog(@"插入成功");
        [db close];
        return;
    }
    
    NSLog(@"插入失败");
    [db close];
    
    
    
}

- (void)blackButtonAction:(UIButton *)sender {
    
}

- (void)reportButtonAction:(UIButton *)sender {
    
}

- (void)leftBarButtonItemAction:(UIBarButtonItem *)sender {
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


#pragma mark 重写dealloc
- (void)dealloc
{
    [_webView release];
    [super dealloc];
   
}


@end
